package com.example.secondhandshop.model;

import android.os.NetworkOnMainThreadException;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.secondhandshop.bean.AllchatitemData;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.bean.ResponseBody;
import com.example.secondhandshop.ui.activity.Constans;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ChatitemModel {
    OkHttpClient client = new OkHttpClient();
    public void getallchatfrom(int userid, int fromuserid,OnProductsFetchedListener listener) {
        new Thread(() -> {
            // url路径
            String url = Constans.SERVER_URL() + "tran/chat/message?fromUserId=+" + fromuserid + "&userId=" + userid+"&size="+30;
            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", Constans.getAppId())
                    .add("appSecret", Constans.getAppSecret())
                    .build();
            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .get()
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.e("ChatitemModel", "onResponse: " + response);
                        if (response.isSuccessful()) {
                            String ChatitemResponseBody = response.body().string();
                            Log.d("ChatitemModel(1)", "API Response Body: " + ChatitemResponseBody);
                            Gson gson = new Gson();
                            ResponseBody<AllchatitemData> responseBody;
                            responseBody = gson.fromJson(ChatitemResponseBody, new TypeToken<ResponseBody<AllchatitemData>>() {
                            }.getType());
                            // 检查响应代码
                            if (responseBody.getCode() == 200) {
                                AllchatitemData allchatitemData = responseBody.getData(); // 直接获取 Data
                                System.out.println("allchatitemData is"+allchatitemData);
                                List<Chatitem> chatitems = allchatitemData.getRecords(); // 获取产品记录
                                System.out.println("chatitems is"+chatitems);
                                listener.onSuccess(chatitems);
                            } else {
                                listener.onFailure(responseBody.getMsg()); // 使用实际消息
                            }
                        } else {
                            listener.onFailure(response.message()); // 捕获请求失败的情况
                        }
                    }
                });
            } catch (NetworkOnMainThreadException ex) {
                ex.printStackTrace();
            }
        }).start();
    }

    public interface OnProductsFetchedListener {
        void onSuccess(List<Chatitem> chatitems);
        void onFailure(String error);
    }
}
